package com.lcq.study.utils;

/**
 * @ClassName MyUtils
 * @Description
 * @Author lichuanqi
 * @Date 2022/1/26 10:57
 * @Version 1.0
 **/
public class MyUtils {
    public static void main(String[] args) {
//        String regex = "[(]";
//        String[] split = taskName1.split(regex);
//        int start = taskName1.lastIndexOf("(");
//        System.out.println(start);
//        int end = taskName1.lastIndexOf(")");
//        System.out.println(end);
//        String substring = taskName1.substring(start+1, end);
//        System.out.println(substring);
//        System.out.println(substring.startsWith("复制-"));
//        if (substring.startsWith("复制-")){
//            String[] split1 = substring.split("-");
//            System.out.println(split1.length);
//            if (split1.length == 2){
//                Integer num = Integer.valueOf(split1[1]);
//                System.out.println(num);
//                String result = String.format("%s-%d", "复制", ++num);
//                System.out.println(result);
//            }
//        }

        String taskName1 = "测试任务(；啊啊)(复制-1)";
        System.out.println(getCopyTaskName(taskName1));
        String taskName2 = "测试任务";
        System.out.println(getCopyTaskName(taskName2));
        String taskName3 = "测试任务(复制-haha)";
        System.out.println(getCopyTaskName(taskName3));
        String taskName4 = "测试任务(复制-4)";
        System.out.println(getCopyTaskName(taskName4));

    }

    public static String getCopyTaskName(String taskName){
        String result = taskName+"(复制-1)";
        if (taskName == null){
            return result;
        }

        try{
            int start = taskName.lastIndexOf("(");
            if (start < 0){
                return result;
            }
            int end = taskName.lastIndexOf(")");
            if (end < 0){
                return result;
            }

            String substring = taskName.substring(start+1, end);
            if (substring.startsWith("复制-")){
                String[] split = substring.split("-");
                if (split.length == 2){
                    Integer num = Integer.valueOf(split[1]);
                    return String.format("%s(%s-%d)", taskName.substring(0, start),"复制", ++num);
                }
            }
            return result;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return result;
        }
    }
}
